@file:Suppress("unused")

package com.hjh.tool.util

import android.app.DatePickerDialog
import android.content.Context
import android.content.DialogInterface
import androidx.core.content.ContextCompat
import com.hjh.res.R
import java.util.Calendar
import java.util.Locale

/**
 * @author： hjh
 * @createDate： 2024/11/28 15:58
 * @description：
 * @qq： 3472903821
 * @version: 1.0
 */
object MyDialogUtil {

    interface DataPickerDialogListener {
        fun onCancel() {}

        fun onConfirm(year: Int?, month: Int?, day: Int?) {}

        /**
         * 格式化后日期 (yyyy-MM-dd HH:mm:ss)
         */
        fun onConfirm(date: String?) {}
    }

    fun showDatePicker(context: Context, dataPickerDialogListener: DataPickerDialogListener?) {

        // 弹出对话框
        val calendar = Calendar.getInstance(Locale.CHINA)
        val datePickerDialog = DatePickerDialog(
            context,
            R.style.DataPickerDialogTheme,
            null,
            calendar[Calendar.YEAR],
            calendar[Calendar.MONTH],
            calendar[Calendar.DAY_OF_MONTH]
        )

        datePickerDialog.setButton(
            DialogInterface.BUTTON_NEGATIVE,
            "取消"
        ) { _: DialogInterface?, _: Int ->
            dataPickerDialogListener?.onCancel()
            datePickerDialog.dismiss()
        }

        datePickerDialog.setButton(
            DialogInterface.BUTTON_POSITIVE,
            "确认"
        ) { _: DialogInterface?, _: Int ->
            // 确认年月日
            val year = datePickerDialog.datePicker.year
            val monthOfYear = datePickerDialog.datePicker.month + 1
            val dayOfMonth = datePickerDialog.datePicker.dayOfMonth

            val format = (if (year < 10) "0$year" else year.toString() + "") + "-" +
                    (if (monthOfYear < 10) "0$monthOfYear" else monthOfYear.toString() + "") + "-" +
                    (if (dayOfMonth < 10) "0$dayOfMonth" else dayOfMonth.toString() + "")
            dataPickerDialogListener?.onConfirm(year, monthOfYear, dayOfMonth)
            dataPickerDialogListener?.onConfirm(format)
        }
        datePickerDialog.show()
        val color = ContextCompat.getColor(context, R.color.colorPrimary)
        datePickerDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(color)
        datePickerDialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(color)
    }
}